package com.wuyou.mid;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 1807. 替换字符串中的括号内容
 */
public class Evaluate {

    public static void main(String[] args) {
        String s = "(xueu)(zify)(ali)";
        String[][] k = {{"xueu","vrje"},{"zify","pfo"},{"ali","qdx"}};
        List<List<String>> knowledge = new ArrayList<List<String>>();
        for (int i = 0; i < k.length; i++) {
            List objects = Arrays.asList(Arrays.stream(k[i]).toArray());
            knowledge.add(objects);
        }
        System.out.println(solution_2(s, knowledge));
    }

    // 最快题解
    public static String solution_2(String s, List<List<String>> knowledge) {
        Map<String, String> map = new HashMap<String, String>();
        knowledge.forEach(k -> map.put(k.get(0), k.get(1)));
        int index = 0, length = s.length();
        StringBuilder res = new StringBuilder();
        while (index < length) {
            char c = s.charAt(index);
            if (c == '(') {
                StringBuilder sb = new StringBuilder();
                while (s.charAt(++index) != ')'){
                    sb.append(s.charAt(index));
                }
                index += 1;
                String s1 = map.get(sb.toString());
                res.append(s1 == null ? "?" : s1);
            } else {
                res.append(c);
                index++;
            }
        }
        return res.toString();
    }


    // 走不通  会出现跳过中间的括号
    public static String replaceStr(String target, String str, String after) {
        while (true) {
            int index = target.indexOf(str);
            if (index == -1) {
                break;
            }
            String substring = target.substring(0, index);
            String substring1 = target.substring(index + str.length());
            target = substring + after + substring1;
        }
        return target ;
    }

    public static String solution_1(String s, List<List<String>> knowledge) {
        Map<String, String> map = new HashMap<String, String>();
        knowledge.forEach(k -> {
            map.put(k.get(0), k.get(1));
        });
        StringBuilder res = new StringBuilder();
        char[] chars = s.toCharArray();
        int index = 0;
        while (index < chars.length) {
            if (chars[index] == '(') {
                StringBuilder sb = new StringBuilder();
                for (int i = index + 1; i < chars.length; i++) {
                    index = i;
                    if (chars[i] == ')') {
                        break;
                    }
                    sb.append(chars[i]);
                }
                index += 1;
                String s1 = map.get(sb.toString());
                if (s1 != null) {
                    res.append(s1);
                } else {
                    res.append("?");
                }
            } else {
                res.append(chars[index]);
                index++;
            }
        }
        return res.toString();
    }
}
